//
//  SecondViewController.m
//  22-ALiStar
//
//  Created by Snow on 16/8/16.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "SecondViewController.h"
#import "MyTableViewCell.h"
#import "TableViewCellOfCollectionView.h"
#import "NetWorking.h"
#import "ModelOfDetail.h"
#import "ModelOfSongList.h"
#import "UIImageView+WebCache.h"
@interface SecondViewController ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) ModelOfDetail *model;
@property (nonatomic, strong) NSMutableArray *arrayOfSongList;
@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"someone";
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor cyanColor];
    [self handleData];
    [self createView];
    
}
- (void)handleData
{
    _arrayOfSongList = [NSMutableArray array];
    NSString *string = _stringOfValue;
    NSString *url = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@", string];
    [NetWorking GETWithURL:url parameters:nil success:^(id responseObject) {
        id result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dic = result[@"data"];
        _model = [[ModelOfDetail alloc] init];
        [_model setValuesForKeysWithDictionary:dic];
        for (NSDictionary *dictionary in _model.songList) {
           ModelOfSongList *modelSong = [[ModelOfSongList alloc] init];
            [modelSong setValuesForKeysWithDictionary:dictionary];
            [_arrayOfSongList addObject:modelSong];
        }
    } filed:^(NSError *error) {
        
    }];
}
- (void)createView
{
    NSLog(@"----------%@", _model);
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[MyTableViewCell class] forCellReuseIdentifier:@"cellOfTable"];
    [self.tableView registerClass:[TableViewCellOfCollectionView class] forCellReuseIdentifier:@"cellOfCollection"];
    [self.view addSubview:self.tableView];
    UIImageView *view  = [[UIImageView alloc] init];
    view.frame = CGRectMake(0, 0, self.view.frame.size.width, 200);
    [view sd_setImageWithURL:[NSURL URLWithString:_model.picUrl]];
    UIImageView *imageOfSinger = [[UIImageView alloc] init];
    imageOfSinger.frame = CGRectMake(10, view.frame.size.height - 60, 50, 50);
    [imageOfSinger sd_setImageWithURL:[NSURL URLWithString:_model.singerPicUrl]];
    [view addSubview:imageOfSinger];
    imageOfSinger.layer.masksToBounds = YES;
    imageOfSinger.layer.cornerRadius = 25;
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(70, view.frame.size.height - 50, 50, 20)];
    label.text = @"EXO";
    [view addSubview:label];
    UILabel *labelOfDate = [[UILabel alloc] initWithFrame:CGRectMake(70, view.frame.size.height - 30, view.frame.size.width - 120, 20)];
    labelOfDate.text = @"2016-6-21";
    label.textColor = [UIColor whiteColor];
    labelOfDate.textColor = [UIColor whiteColor];
    [view addSubview:labelOfDate];
    self.tableView.tableHeaderView = view;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0 || section == 2) {
        return 0;
    } else if(section == 1){
        return 7;
    } else {
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        TableViewCellOfCollectionView *cell = [tableView dequeueReusableCellWithIdentifier:@"cellOfCollection"];
        return cell;
    }
    MyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellOfTable"];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 3) {
        return 20;
    }
    return 50;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 3) {
        return 120;
    }
    return 60;
    
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        for (int i = 0; i < 3; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i * self.view.frame.size.width / 3 + self.view.frame.size.width / 10, 5, self.view.frame.size.width / 12, 20);
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(i * self.view.frame.size.width / 3 + self.view.frame.size.width / 10, 30, self.view.frame.size.width / 10, 10)];
            label.text = @"1234";
            label.font = [UIFont systemFontOfSize:10];
            [button setImage:[UIImage imageNamed:@"pic_1"] forState:UIControlStateNormal];
            [view addSubview:label];
            [view addSubview:button];
        }
        return view;
    } else if(section == 1) {
   UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(10, 10, 20, 20);
        [button setImage:[UIImage imageNamed:@"pic_1"] forState:UIControlStateNormal];
        [view addSubview:button];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(40, 15, 120, 20)];
        label.text = @"随机播放";
        label.font = [UIFont systemFontOfSize:12];
        [view addSubview:label];
        return view;
    } else if (section == 2) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 50)];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(view.frame.size.width - 50, 10, 50, 30)];
        label.text = @"15人收藏";
        label.font = [UIFont systemFontOfSize:12];
        [view addSubview:label];
        return view;
    } else {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 20)];
        UILabel *label = [[UILabel alloc] initWithFrame:view.frame];
        label.text = @"EXO更多专辑";
        label.font = [UIFont systemFontOfSize:15];
        [view addSubview:label];
        return view;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
